package net.slidinglive.utilities.adapter;

import java.util.ArrayList;
import java.util.List;

import net.slidinglive.utilities.R;
import net.slidinglive.utilities.model.Address;
import net.slidinglive.utilities.model.Contact;
import net.slidinglive.utilities.model.EmailAddress;
import net.slidinglive.utilities.model.PhoneNumber;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author Sebastian Hoffmann <bo.hoffi@gmail.com>
 */
public class ContactAdapter
        extends BaseAdapter {

    private final Context context;
    private final List<Contact> contacts;

    public ContactAdapter(final Context context) {

        this.context = context;
        this.contacts = new ArrayList<Contact>();
    }

    public void add(final Contact contact) {

        this.contacts.add(contact);
        this.notifyDataSetChanged();
    }

    public void addAll(final List<Contact> contacts) {

        for (final Contact contact : contacts) {
            this.contacts.add(contact);
        }
        this.notifyDataSetChanged();
    }

    public void remove(final int id) {

        this.contacts.remove(this.getItemById(id));
        this.notifyDataSetChanged();
    }

    public void removeAll() {

        this.contacts.clear();
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {

        return this.contacts.size();
    }

    @Override
    public Contact getItem(int position) {

        return this.contacts.get(position);
    }

    public Contact getItemById(final int id) {

        Contact contact = null;

        for (final Contact c : this.contacts) {
            if (c.getId() == id) {
                contact = c;
            }
        }

        return contact;
    }

    @Override
    public long getItemId(int position) {

        return this.contacts.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final LayoutInflater vi = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.adapter_contact, null);

        final Contact contact = this.getItem(position);

        final TextView displayNameTextView = (TextView) convertView.findViewById(R.id.adapter_contact_display_name);
        displayNameTextView.setText(contact.getDisplayName());

        final LinearLayout phoneContainer =
                (LinearLayout) convertView.findViewById(R.id.adapter_contact_phone_container);
        phoneContainer.setOrientation(1);

        for (final PhoneNumber phoneNumber : contact.getPhoneNumbers()) {
            phoneContainer.addView(this.createSimpleContainerItemView(
                    this.context.getResources().getString(phoneNumber.getTypeLabelResourceId()),
                    phoneNumber.getNumber()));
        }

        final int emailCount = contact.getEmailAddresses().size();

        final TextView emailContainerHeader =
                (TextView) convertView.findViewById(R.id.adapter_contact_email_container_header);
        emailContainerHeader.setVisibility((emailCount > 0 ? View.VISIBLE : View.GONE));

        final LinearLayout emailContainer =
                (LinearLayout) convertView.findViewById(R.id.adapter_contact_email_container);
        emailContainer.setOrientation(1);
        emailContainer.setVisibility((emailCount > 0 ? View.VISIBLE : View.GONE));

        for (final EmailAddress emailAddress : contact.getEmailAddresses()) {
            emailContainer.addView(this.createSimpleContainerItemView(
                    this.context.getResources().getString(emailAddress.getTypeLabelResourceId()),
                    emailAddress.getAddress()));
        }

        final int addressCount = contact.getAddresses().size();

        final TextView addressContainerHeader =
                (TextView) convertView.findViewById(R.id.adapter_contact_address_container_header);
        addressContainerHeader.setVisibility((addressCount > 0 ? View.VISIBLE : View.GONE));

        final LinearLayout addressContainer =
                (LinearLayout) convertView.findViewById(R.id.adapter_contact_address_container);
        addressContainer.setOrientation(1);
        addressContainer.setVisibility((addressCount > 0 ? View.VISIBLE : View.GONE));

        for (final Address address : contact.getAddresses()) {
            addressContainer.addView(this.createAddressContainerItemView(address));
        }

        return convertView;
    }

    private View createSimpleContainerItemView(final String headerText, final String contentText) {

        final LayoutInflater vi = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View simpleContainerView = vi.inflate(R.layout.adapter_container_simple_item, null);

        final TextView headerTextView =
                (TextView) simpleContainerView.findViewById(R.id.adapter_container_simple_item_header);
        headerTextView.setText(headerText);
        final TextView contentTextView =
                (TextView) simpleContainerView.findViewById(R.id.adapter_container_simple_item_content);
        contentTextView.setText(contentText);

        return simpleContainerView;
    }

    private View createAddressContainerItemView(final Address address) {

        final LayoutInflater vi = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View simpleContainerView = vi.inflate(R.layout.adapter_contact_container_address_item, null);

        final View x = new View(this.context);

        final TextView typeTextView =
                (TextView) simpleContainerView.findViewById(R.id.adapter_contact_container_address_item_type);
        typeTextView.setText(this.context.getResources().getString(address.getTypeLabelResourceId()));
        final TextView streetTextView =
                (TextView) simpleContainerView.findViewById(R.id.adapter_contact_container_address_item_street);
        streetTextView.setText(address.getStreet());
        final TextView postCodeTextView =
                (TextView) simpleContainerView.findViewById(R.id.adapter_contact_container_address_item_postCode);
        postCodeTextView.setText(address.getPostCode());
        final TextView cityTextView =
                (TextView) simpleContainerView.findViewById(R.id.adapter_contact_container_address_item_city);
        cityTextView.setText(address.getCity());
        final TextView regionTextView =
                (TextView) simpleContainerView.findViewById(R.id.adapter_contact_container_address_item_region);
        regionTextView.setText(address.getRegion());
        final TextView countryTextView =
                (TextView) simpleContainerView.findViewById(R.id.adapter_contact_container_address_item_country);
        countryTextView.setText(address.getCountry());

        return simpleContainerView;
    }
}